<?php

namespace app\common\service;
use app\common\service\AbstractService;
use app\common\service\CommonSystemImportInfoDetailService;
use app\systems\model\CommonSystemImportDetailModel;
use app\systems\model\CommonSystemImportModel;
use app\admin\logic\TestLogic;
use app\admin\logic\TestTimeLogic;
use app\checkbill\logic\BillLogic;
use app\checkbill\logic\BillQcLogic;
use app\admin\logic\uploadExcelTest;
use app\thirdPay\logic\LianlianLogic;
use app\checkbill\logic\CheckMonthFeeLogic;
use app\checkbill\logic\CheckMonthOldLogic;

class CommonSystemImportDetailService extends CommonSystemImportInfoDetailService {

    public function __construct() {
        $this->system_import_model        = new CommonSystemImportModel();
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
    }




    /**
     * incomplete_sku匹配捆绑导入
     * @param int $id
     * @throws \Exception
     * @author:luckyXiong
     * @Date: 2022/10/10
     */
    public function import_incomplete_sku($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new TestLogic())->import_detail($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }


    /**
     * incomplete_sku匹配捆绑导入
     * @param int $id
     * @throws \Exception
     * @author:luckyXiong
     * @Date: 2022/10/10
     */
    public function import_incomplete_time($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new TestTimeLogic())->import_time_detail($importValue);
           // $result = (new BillLogic())->import_sf_info($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }




    /**
     * 顺丰账单数据导入
     * @param int $id
     * @author:luckyXiong
     * @Date: 2024/4/22
     */
    public function import_sf_page($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new BillLogic())->import_sf_info($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }


    /**
     * 全程无忧账单数据导入
     * @param  int $id
     * @author:luckyXiong
     * @Date:  2024/4/22
     */
    public function import_qc_page($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new BillQcLogic())->import_qc_info($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }



    /**
     * 连连账单数据导入
     * @param  int $id
     * @author:luckyXiong
     * @Date:  2024/06/05
     */
    public function import_lian_lian($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new LianlianLogic())->import_info($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }




    /**
     * 月度账单老模版数据导入
     * @param  int $id
     * @author:luckyXiong
     * @Date:  2024/06/06
     */
    public function import_month_old($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new CheckMonthOldLogic())->import_info($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }




    /**
     * 月度账单新模版数据导入
     * @param  int $id
     * @author:luckyXiong
     * @Date:  2024/06/11
     */
    public function import_month_new($id){
        set_time_limit(0);
        $this->system_import_detail_model = new CommonSystemImportDetailModel();
        $this->system_import_model        = new CommonSystemImportModel();
        $detail = $this->system_import_detail_model->find(["status" => self::STATUS_DETAIL_INIT, "id" => $id], false);
        $detail = objectToArray($detail);
        if (empty($detail)) throw new \RuntimeException('状态不正确');
        $import_task = $this->system_import_model->find(["id" => $detail['import_id']]);//可以获取创建人
        $importValue = json_decode($detail['json_info'], true);
        $import_task = objectToArray($import_task);
        $importValue['creat_by'] = $import_task['created_work_no'];
        try {
            $result = (new CheckMonthFeeLogic())->import_info($importValue);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        $status = empty(getIsset($result, 'status')) ? self::STATUS_DETAIL_FAIL : self::STATUS_DETAIL_SUCCESS;
        $detailRes = $this->system_import_detail_model->update(['status' => $status],['id'=>$detail['id']]);
        if (!$detailRes) {
            throw new \Exception('更改状态失败');
        }
        $this->parentStatusChange($detail['import_id']);
    }





























}